<?php 
use Wdl\helper\webHelper;
// 新版本的公司经纪人和会员管理
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl'); 
$uniacid = webHelper::bindUniacid(); 
$openid = $_W['openid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'index';	
$template_name = sprintf("sbroker/%s",  $operation);

$rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';	
 
// var_dump($_W); exit;
/* 设置经纪人的属性*/
if($operation=='setproperty'){
    
    $id = intval($_GPC['id']);
    $type = $_GPC['type'];
    $data = intval($_GPC['data']);
    if (in_array($type, array('disabled', 'isyou', 'ischeng'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_broker", array(  $type => $data), array("id" => $id, "uniacid" => $uniacid ));
            die(json_encode(array("result" => 1,'msg'=> $data==1? '禁用':'正常',  "data" => $data)));
    }
 
    die(json_encode(array("result" => 0)));
    /**编辑会员资料 */
}elseif($operation =='editbroker'){
    if(!empty($_GPC['id'])){
        $params = array(':uniacid' => $uniacid,':id'=> intval($_GPC['id']) );
        $item = pdo_fetch("select * from ".tablename("kbwdl_broker")." where uniacid=:uniacid and id=:id", $params);
    }
    if(checksubmit('submit')){
        $username = $_GPC['data']['nickname'];
        $savedata = $_GPC['data'];
        if(!empty($_GPC['thumb'])){
           $savedata['avatar'] = $_GPC['thumb'];
        }
         $savedata['end_time'] =  $_GPC['end_time'];
        
        if($savedata['shopid']>0){
           $r  = pdo_fetch("select uid,shopname from ".tablename("kbwdl_shop")." where id='".$savedata['shopid']."'");
           
           $savedata['company'] = $r['shopname'];              
        }    
        /*公司头衔*/         
        if(isset($savedata['cgid']) && !empty($savedata['cgid'])){
            $savedata['cgid'] = intval($savedata['cgid']);
        }else{
            $savedata['cgid'] = 0;
        }
       
        $desc = $_GPC['data']['desc'];
        
       /**/
        pdo_update( 'kbwdl_broker', 
                $savedata,             
                array('uniacid' => $uniacid,'id'=> intval($_GPC['id']) )           
         );
        $bid = intval($_GPC['id']) ;
        /**/
        if(!empty($_GPC['referer'])){
            $return_url = $_GPC['referer'];
        }else{
             $return_url = $this->createWebUrl('broker' );
        }
        message("修改成功", $return_url , 'success');
    }
}

$condition = ' WHERE `uniacid` = :uniacid  ';
$params = array(':uniacid' => $uniacid );
    /*类型*/
$rt = intval($_GPC['rt']);
if(empty($rt)){
    $rt = 1;
}
if($operation =='index'){
    //普通会员
    $condition.= "  and iscompany = 0";
}else{
    //公司经纪人
    $condition.=" and iscompany>0 ";
}
 
if (!empty($_GPC['keyword'])) {
    $condition .= ' AND (`nickname` LIKE :username or company like :username or mobile like :username or openid like :username)';
    $params[':username'] = '%' . trim($_GPC['keyword']) . '%';
}
//获取列表数据
$fd = webHelper::table_list_data('kbwdl_broker',$condition, $params);
$total = $fd['total'];
$list  = $fd['list'];
$pager = $fd['pager'];


$cgid_option =  [0=>'普通会员',1=>'销售经理', 2=>'金牌销售',3=>'金牌顾问',4=>'销售顾问',5=>'金牌经理'];
 //下面是公共数据
 $submenus = [
    'index'=>['name'=> '普通用户'],
    'broker'=>['name'=> '经纪人'],
];
if($operation == 'index' || $operation == 'broker' ){
    $template_name = 'sbroker/index';
}
$admin = get_shop_permission($uniacid); 
/**
 * 查找到模块的管理员账号
 */
function get_shop_permission($uniacid){
    global $_W,$GPC; 
    $opersql = '';
    if($_W['user']['type']==3){    
        $opersql = " AND  `uid` ='".$_W['uid']."' ";        
    }
    
    $role = pdo_fetchall("select shopname,id from ".tablename("kbwdl_shop").
            "  where uniacid='$uniacid'  $opersql");
    $admin = array();
    if(!empty($role)){
        foreach($role as $key=>$r){
            $admin[$r['id']] = $r['shopname'];
        }
    }
    return $admin;
}

include $this->template('sbroker/layout');